<?php
namespace M\Controller;
use Think\Controller;
class AcnewyearController extends CommonController {

	/**
	 * 制作问题选项
	 */
	public function create_options(){


		if(IS_GET){

			/**
			 * 检测用户是否关注
			 */
			$openid = $this->memberinfo['wxopenid'];
//                        $openid = "ojEgswCMN8WAOQinXEe-7fl8aZ2M";
			$res = $this->chk_member_is_fan_s($openid);
			if($res){
				// echo 123; die;
				$is_fan = 1;
				$this->assign('is_fan',$is_fan);
			}else{
				$is_fan = 0;
				$this->assign('is_fan',$is_fan);
			}

			//获取随机6道答案选项
			$random_answers = $this->get_random_answers();
			// print_r($random_answers); die;

			//获取好友猜题结果
			$friend_guess = $this->get_friend_guess();
			// print_r($friend_guess); die;
			//获取我的竞猜结果
			$my_guess = $this->get_my_guess();

			// print_r($friend_guess); die;
			$this->assign('random_answers',$random_answers);
			$this->assign('friend_guess',$friend_guess);
			$this->assign('my_guess',$my_guess);
			$this->display();
		}else{
			// print_r($_POST); die;
			$data['option1'] = I('post.option1');
			$data['option2'] = I('post.option2');
			$data['option3'] = I('post.option3');
			$data['option4'] = I('post.option4');
			$data['option5'] = I('post.option5');
			$data['option6'] = I('post.option6');
			$data['right_option'] = I('post.right_option');
			$data['mid'] = $this->memberinfo['id'];
			// $data['openid'] = $this->memberinfo['openid'];
			$data['ctime'] = I('server.REQUEST_TIME');

			$DB = M('activity_newyear_item_content');
			// print_r($data); die;
			$res = $DB->data($data)->add();
			// echo $res;
			if(!empty($res)){
				// $url = U('M/Acnewyear/create_options_end',array('id'=>$res,'from_mid'=>$this->memberinfo['id']));
				$this->redirect('Acnewyear/create_options_end',array('id'=>$res,'from_mid'=>$this->memberinfo['id']));
			}
		}
	}


	/**
	 * 制作完成页面
	 */
	public function create_options_end(){


		$id = I('get.id');
		$from_mid = I('get.from_mid');
                
               //获取用户的头像信息
                $member = M("members")->find($from_mid);
                if(empty($member))
                {
                    redirect(U('M/Acnewyear/create_options'));
                }
                else
                {
                    $this->assign('member', $member);
                }

		$this->assign('id',$id);
		$this->assign('from_mid',$from_mid);
		$this->display();
	}


	/**
	 * 猜题页面
	 */
	public function guess_options(){

		if(IS_GET){
			// print_r($this->memberinfo); die;
			$id = I('get.id');
			$from_mid = I('get.from_mid');
			$to_mid = $this->memberinfo['id'];

			/**
			 * 获取正确的option
			 */
			// echo $id; die;
			$right_option = $this->get_right_option($id);
			// echo $right_option; die;
			/**
			 * 判断是否是本人打开
			 */
			if($from_mid == $to_mid){

				$this->redirect('Acnewyear/create_options');
				return;
			}
			/**
			 * 判断用户是否回答过
			 */
			$res = M('activity_newyear_result_record')->where(array('cid'=>$id,'from_mid'=>$from_mid,'to_mid'=>$to_mid))->find();
			// print_r($res); die;
			if($res){

				if($res['result'] == 1){

					/**
					 * 获取用户以前回答的答案
					 */
					$list = M('activity_newyear_item_content')->where(array('id'=>$id))->find();
					$history_check_option = M('activity_newyear_result_record')->where(array('cid'=>$id,'from_mid'=>$from_mid,'to_mid'=>$to_mid))->getField('option');
					foreach ($list as $key => $value) {

						if($value == $history_check_option){

						$list[$key] = '<div style="border: 1px solid red;" class="blend-panel-center right_size history_check_option">
	      								<p><span>'.$value.'</span></p></div>';
	  
						}else{

							$list[$key] = '<div class="blend-panel-center right_size history_check_option">
	          								<p><span>'.$value.'</span></p></div>';
						}
					}
					$this->assign('list',$list);
					$this->display('Acnewyear/right_option_result');
					return; 
				}else if($res['result'] == 2){

					/**
					 * 获取用户以前回答的答案
					 */
					$list = M('activity_newyear_item_content')->where(array('id'=>$id))->find();
					$history_check_option = M('activity_newyear_result_record')->where(array('cid'=>$id,'from_mid'=>$from_mid,'to_mid'=>$to_mid))->getField('option');
					// echo $history_check_option; die;
					foreach ($list as $key => $value) {
						# code...
						if($value == $history_check_option){

							$list[$key] = '<div style="border: 1px solid red;" class="blend-panel-center right_size history_check_option">
              								<p><span>'.$value.'</span></p></div>';
          
						}else{

							$list[$key] = '<div class="blend-panel-center right_size history_check_option">
              								<p><span>'.$value.'</span></p></div>';
						}
					}
					// print_r($list); die;
					$this->assign('list',$list);
					$this->display('Acnewyear/error_option_result');
					return;
				}
			}else{
				// echo 123; die;
				$DB_item = M('activity_newyear_item_content');

				$list = $DB_item->where(array('id'=>$id))->find();
                                if(empty($list))
                                {
                                    redirect(U('M/Acnewyear/create_options'));
                                }
                                else
                                {
                                    $list['from_user_info'] = M('members')->where(array('id' => $list['mid']))->field('id,username,avatar,wxopenid')->find();

                                    $this->assign('right_option',$right_option);
                                    $this->assign('list',$list);
                                    $this->display();
                                }
				
			}
		}
	}


	/**
	 * 提交猜测结果
	 */
	public function guess_options_result(){

		if(IS_GET){

			$data['cid'] = I('get.cid');
			$data['from_mid'] = I('get.from_mid');
			$data['from_openid'] = I('get.from_openid');
			$data['to_mid'] = $this->memberinfo['id'];
			$data['to_openid'] = $this->memberinfo['wxopenid'];
			$option_check = I('get.option_check');
			$data['option'] = I('get.option');
			$data['ctime'] = I('server.REQUEST_TIME');
			$the_right_option = I('get.right_option');

			$res = M('activity_newyear_result_record')->where(array('cid'=>$data['cid'],'from_mid'=>$data['from_mid'],'to_mid'=>$data['to_mid']))->find();
			if($res){

				$this->redirect('Acnewyear/create_options');
			}
			// echo $right_option; die;
			//查询用户提交的选项结果是否正确
			$DB_item = M('activity_newyear_item_content');
			$right_option = $DB_item->where(array('id'=>$data['cid']))->getField('right_option');
			if($right_option == $option_check){

				$data['result'] = 1;
				$list['result'] = 1;
			}else{

				$data['result'] = 2;
				$list['result'] = 2;
			}

			M('activity_newyear_result_record')->data($data)->add();

			// print_r($list); die;
			$this->assign('check_option',$option);
			$this->assign('the_right_option',$the_right_option);
			$this->assign('list',$list);
			$this->display();
		}
	}

	/**
	 * 正确页面
	 */
	public function right_option_result(){

		$this->display();
	}

	/**
	 * 错误页面
	 */
	public function error_option_result(){

		$this->display();
	}




	/**
	 * 随机获取6个备选答案
	 */
	private function get_random_answers(){

		$DB = M('activity_newyear_alternative_answers');

		// $max_num = $DB->max('id');

		// $min_num = $DB->min('id');
		$list = $DB->field('item')->select();

		$keys = array_rand($list,6);

		foreach ($keys as $key => $value) {
			# code...
			$arr[] = $list[$value];
		}

		return $arr;
	}


	/**
	 * 获取好友猜题
	 */
	private function get_friend_guess(){

		$DB_result = M('activity_newyear_result_record');

		$DB_members = M('members');

		$result_map['from_mid'] = $this->memberinfo['id'];
		
		$arr = $DB_result->where($result_map)->order("ctime desc")->select();

		foreach ($arr as $key => $value) {
			# code...
			$arr[$key]['avatar'] = $DB_members->where(array('id' => $value['to_mid']))->getField('avatar');
			$arr[$key]['to_username'] = $DB_members->where(array('id' => $value['to_mid']))->getField('username');
                        $arr[$key]['to_username'] = mb_substr($arr[$key]['to_username'], 0,4,'utf-8');
		}
		// print_r($arr);
		// 
		return $arr;
	}



	/**
	 * 获取我的竞猜结果
	 */
	private function get_my_guess(){

		$DB_result = M('activity_newyear_result_record');

		$DB_members = M('members');

		$result_map['to_mid'] = $this->memberinfo['id'];
		
		$arr = $DB_result->where($result_map)->order("ctime desc")->select();

		foreach ($arr as $key => $value) {
			# code...
			$arr[$key]['from_username'] = $DB_members->where(array('id' => $value['from_mid']))->getField('username');
			$arr[$key]['to_username'] = $DB_members->where(array('id' => $value['to_mid']))->getField('username');
			$arr[$key]['avatar'] = $DB_members->where(array('id' => $value['to_mid']))->getField('avatar');
                        $arr[$key]['to_username'] = mb_substr($arr[$key]['to_username'], 0,4,'utf-8');
                        $arr[$key]['from_username'] = mb_substr($arr[$key]['from_username'], 0,4,'utf-8');
		}
		// print_r($arr);
		// 
		return $arr;
	}

	/**
	 * 获取正确的选项
	 */
	private function get_right_option($id){

		$DB = M('activity_newyear_item_content');
		$map['id'] = $id;

		$res = $DB->where($map)->find();

		$right_option_key = 'option'.$res['right_option'];

		$right_option = $res[$right_option_key];

		return $right_option;
	}



	/**
	 * ajax检测用户是否关注
	 */
    public  function chk_member_is_fan() {

        if(IS_AJAX){

        	$openid = $this->memberinfo['wxopenid'];
        	$res = $this->chk_member_is_fan_s($openid);
	        if(!empty($res))
	        {
	            echo json_encode(1);
	        }
	        else
	        {
	            echo json_encode(0);
	        }
        }
    }

    /**
     * 判断用户是否关注
     */
    
	private function chk_member_is_fan_s($openid){

		$DB = M("members_subscribe_records");
        
        $map['openid'] = $openid;
        $map['issubscribe']=1;
        
        $res = $DB->where($map)->find();

        return $res;
	}



	/**
	 * 活动统计函数
	 */
	public function get_statistics_data(){

		if(IS_GET){

			$DB_item = M('activity_newyear_item_content');
			$DB_result = M('activity_newyear_result_record');

			//获取发起问答总条目数
			$activity_newyear_item_count = $DB_item->count();
			// echo $activity_newyear_item_count;
			//获取发起活动总人数
			$sql1 = "select count(*) from (select count(*) from fd_activity_newyear_item_content group by mid) as view";
			$activity_newyear_item_member = $DB_item->query($sql1);
			$activity_newyear_item_member_count = $activity_newyear_item_member[0]['count(*)'];
			
			//参与回答的人次
			$activity_newyear_record_count = $DB_result->count();

			//参与回答的人数
			$sql2 = "select count(*) from (select count(*) from fd_activity_newyear_result_record group by to_mid) as view";
			$activity_newyear_record_member = $DB_result->query($sql2);
			$activity_newyear_record_member_count = $activity_newyear_record_member[0]['count(*)'];

			echo '猜一猜活动发起总次数:'.$activity_newyear_item_count."<br />";
			echo '猜一猜活动发起总人数:'.$activity_newyear_item_member_count."<br />";
			echo '猜一猜活动问答总次数:'.$activity_newyear_record_count."<br />";
			echo '猜一猜活动问答总人数'.$activity_newyear_record_member_count."<br />";
		}
	}
        
        public function show_device_screen() {
            $this->display();
        }


}